package com.fk.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fk.reggie.common.CustomException;
import com.fk.reggie.domain.Dish;
import com.fk.reggie.domain.DishFlavor;
import com.fk.reggie.dto.DishDto;
import com.fk.reggie.service.DishFlavorService;
import com.fk.reggie.service.DishService;
import com.fk.reggie.mapper.DishMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 凡凯
 * @description 针对表【dish(菜品管理)】的数据库操作Service实现
 * @createDate 2023-03-16 10:26:50
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
        implements DishService {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品
        dishService.save(dishDto);
        Long dishId = dishDto.getId();
        //保存口味的菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();
        if (flavors != null && flavors.size() != 0) {
            flavors = flavors.stream().peek(i -> i.setDishId(dishId)).collect(Collectors.toList());
            //保存菜品口味
            log.info(flavors.toString());
            dishFlavorService.saveBatch(flavors);
        }
    }

    @Override
    public DishDto getDishWithFavor(Long id) {
        Dish dish = dishService.getById(id);

        DishDto dishDto = new DishDto();
        //查询dish
        BeanUtils.copyProperties(dish, dishDto);

        //查询dish的口味
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, id);

        List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper);

        dishDto.setFlavors(dishFlavorList);

        return dishDto;
    }

    @Override
    @Transactional
    public void updateDishWithFavor(DishDto dishDto) {
        //更新菜品信息
        dishService.updateById(dishDto);
        //删除之前菜品口味
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lambdaQueryWrapper);
        //保存新提交的菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        //设置口味所属菜品
        if (flavors != null && flavors.size() != 0) {
            flavors = flavors.stream().map(dishFlavor -> {
                dishFlavor.setDishId(dishDto.getId());
                return dishFlavor;
            }).collect(Collectors.toList());
            //保存菜品口味
            log.info(flavors.toString());
            dishFlavorService.saveBatch(flavors);
        }
    }

    @Override
    @Transactional
    public void updateStatusByBatch(Integer status, List<Long> ids) {
        UpdateWrapper<Dish> dishUpdateWrapper = new UpdateWrapper<>();
        dishUpdateWrapper.set("status", status)
                .in("id", ids);
        dishService.update(dishUpdateWrapper);
    }

    @Override
    @Transactional
    public void deleteDish(List<Long> ids) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId, ids)
                .eq(Dish::getStatus, 1);
        int count = dishService.count(dishLambdaQueryWrapper);
        if (count > 0) {
            throw new CustomException("菜品正在售卖，不允许删除");
        }
        //删除菜品
        dishService.removeByIds(ids);
        //删除菜品口味
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(lambdaQueryWrapper);
    }
}




